<?php
class NoteController extends Controller{
    public $layout='//layouts/admin';

    public function Upload($file){
        $fileupload = $file['file_upload_note'];
        $tmp=$fileupload['tmp_name'];
        $filename=time().$fileupload['name'];
        $destinal='uploads/note/'.$filename;
        move_uploaded_file($tmp,$destinal);
        return $destinal;
    }

    public function Listcategory(){
        $model = new Category();
        $listmodel = $model->findAll("id<=6");
        return $listmodel;
    }

    public function actionPreInsertNote(){
        session_start();
//        echo '<pre>';
//        print_r($_SESSION);
//        echo '<pre/>';
//        die;
        if(isset($_SESSION['admin'])){
            $listcategory = $this->Listcategory();
            $model = new Note();
            $this->render('insert_note',array('model' => $model,'listcategory'=>$listcategory));
        }else{
            $this->redirect('index.php?r=admin/admin/prelogin');
        }
    }

    public function actionInsertNote(){
        session_start();
        $images = $this->Upload($_FILES);
        if(isset($_SESSION['admin'])){
            $model = new Note();
            $model->title=$_POST['title'];
            $model->subtitle=$_POST['subtitle'];
            $model->images=$images;
            $model->content=$_POST['Note']['content'];
            $model->category_id=$_POST['category_id'];
            $model->admin_id=$_SESSION['admin']['id'];
            $model->updatetime = date("Y-m-d H:i:s") ;
            $model->save();
            $this->redirect('index.php?r=admin/note/listnote');
        }else{
            $this->redirect('index.php?r=admin/note/listnote');
        }
    }

    Public Function actionListNote(){
        session_start();
        if(isset($_SESSION['admin'])){
            $model = new Note();
            $listnote = $model->findAll();
            $this->render('listnote',array('listnote'=>$listnote));
        }else{
            $this->redirect('index.php?r=admin/admin/prelogin');
        }
    }
    public Function actionPreUpdateNote(){
        session_start();
        if(is_numeric($_GET['id']) && isset($_SESSION['admin'])){
            echo $id = $_GET['id'];
            $model= Note::model()->FindByPk($id);
            $noteitem = $model->FindByPk($id);
            $listcategory = $this->Listcategory();
            $_SESSION['note']['id'] = $noteitem->id;
            $this->render('update_note',array('noteitem'=>$noteitem, 'model' => $model, 'listcategory'=>$listcategory));
        }else{
            $this->redirect('index.php?r=admin/admin/prelogin');
        }
    }

    public function actionUpdateNote(){
        session_start();
        $images = $this->Upload($_FILES);
        if(isset($_SESSION['admin'])){
            $id = $_SESSION['note']['id'];
            $model = Note::model()->FindByPk($id);
            $model->title = $_POST['title'];
            $model->subtitle=$_POST['subtitle'];
            $model->content=$_POST['Note']['content'];
            $model->category_id=$_POST['category_id'];
            if($_FILES['file_upload_note']['name']){
                $model->images=$images;
            }
            $model->admin_id=$_SESSION['admin']['id'];
            $model->update();
            $this->redirect('index.php?r=admin/note/listnote');
        }else{
            $this->redirect('index.php?r=admin/admin/prelogin');
        }
    }
    public function actionDeleteNote(){
        $id = $_GET['id'];
        session_start();
        $model = Note::model()->FindByPk($id);
        $model->admin_id;
        if($_SESSION['admin']['lever']==1 || $model->admin_id == $_SESSION['admin']['id']){
            $check = $model->delete();
            Yii::app()->user->setFlash('contact','xóa thành công');
            $this->redirect('index.php?r=admin/note/listnote');
        }
        else{
            Yii::app()->user->setFlash('contact','xóa thất bại');
            $this->redirect('index.php?r=admin/note/listnote');
        }
    }
}